<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
      @cancel="$emit('close')"
    />
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'update-birthday',
  components: {},
  //   接收父组件传过来的数据
  props: {
    value: String
  },
  data () {
    return {
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  created () {},
  mounted () {},
  methods: {
    //   确定操作
    async  onConfirm (value) {
      const birthday = dayjs(value).format('YYYY-MM-DD')
      try {
        // 发请求
        await updateUserName({ birthday })
        //   提示成功
        this.$toast.success('操作成功')
        //   更新父组件数据
        this.$emit('input', birthday)
        // 关闭弹层
        this.$emit('close')
      } catch (e) {
        console.log(e)
        this.$toast(e.message)
      }
    }
  }
}
</script>

<style>
</style>